<?php

class PhotoModel extends Model {
        function upload($field){
		//mkdir("img/".$field['username'],0777);

		//save uploaded file
		if ($field["pp"]["error"] > 0){
			//do nothing
		}else{
			if (file_exists("img/" .$field['username'].'/'.$field["pp"]["name"])) {
				//echo $field["pp"]["name"] . " already exists. ";
                                $pict_url = "img/" .$field['username'].'/'. $field["pp"]["name"];
			}
			else{
				move_uploaded_file($field["pp"]["tmp_name"],"img/" .$field['username'].'/'. $field["pp"]["name"]);
				$pict_url = "img/" .$field['username'].'/'. $field["pp"]["name"];
			}
		}

		//do the query
		$insert_string = "'".$field['iduser']."','".$pict_url."','".$field['deskripsi']."','".$field['tgl_foto']."','".$field['idkategori']."','".$field['hak_akses']."','".$field['hak_komen']."'";
                //'5',  'linkpic',  'deskripsi',  '2011-03-24', '2',  '2'
		$this->query('INSERT INTO  foto (iduser ,pic ,deskripsi ,tgl_foto ,idkategori ,hak_akses, hak_komen)VALUES ('.$insert_string.')');
                //$myquery = "insert into user (Fullname,Username,Password,Tgllahir,Email,Pp,Gender,Kota) values (".$insert_string.")";
                $temp = $this->query('select idfoto from foto where pic = \''.mysql_real_escape_string($pict_url).'\'');
                //echo 'insert into katakunci (key) values (\''.mysql_real_escape_string($field['idkey']).'\')';
                $this->query('insert into katakunci (`key`) values (\''.mysql_real_escape_string($field['idkey']).'\')');
//                $temp2 = $this->query('select idkey from katakunci where key = \''.mysql_real_escape_string($field['idkey']).'\'');
//                print_r($temp);
//                print_r($temp2);
                //$this->query('insert into key_foto (idfoto,idkey) values (\''.mysql_real_escape_string($temp[0]['Foto']['idfoto']).'\',\''.mysql_real_escape_string($temp2).'\')');
                //echo $myquery;
	}
}
